package com.bootdo.shop.controller;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.utils.*;
import com.bootdo.shop.domain.WxShopCatDO;
import com.bootdo.shop.service.WxShopCatService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * 微信商品类目
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2021-05-19 14:24:51
 */
 
@Controller
@RequestMapping("/shop/wxShopCat")
public class WxShopCatController {
	@Autowired
	private WxShopCatService wxShopCatService;
	
	@GetMapping()
	@RequiresPermissions("shop:wxShopCat:wxShopCat")
	String WxShopCat(){
	    return "shop/wxShopCat/wxShopCat";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("shop:wxShopCat:wxShopCat")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
        //查询列表数据
        Query query = new Query(params);
	    if(params.containsKey("name")&&StringUtils.isNotBlank(params.get("name").toString())){
            List<WxShopCatDO> wxShopCatList = wxShopCatService.likeList(query);
            int total = wxShopCatList.size();
            PageUtils pageUtils = new PageUtils(wxShopCatList, total);
            return pageUtils;
		}

		List<WxShopCatDO> wxShopCatList = wxShopCatService.list(query);
		int total = wxShopCatService.count(query);
		PageUtils pageUtils = new PageUtils(wxShopCatList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("shop:wxShopCat:add")
	String add(){
	    return "shop/wxShopCat/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("shop:wxShopCat:edit")
	String edit(@PathVariable("id") String id,Model model){
		WxShopCatDO wxShopCat = wxShopCatService.get(id);
		model.addAttribute("wxShopCat", wxShopCat);
	    return "shop/wxShopCat/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shop:wxShopCat:add")
	public R save( WxShopCatDO wxShopCat){
	    wxShopCat.setId(UuidUtil.get32UUID());
	    wxShopCat.setDelFlag(0);
	    wxShopCat.setCaAt(new Date());
		if(wxShopCatService.save(wxShopCat)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shop:wxShopCat:edit")
	public R update( WxShopCatDO wxShopCat){
		wxShopCat.setUpAt(new Date());
		wxShopCatService.update(wxShopCat);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shop:wxShopCat:remove")
	public R remove( String id){
		WxShopCatDO wxShopCat = wxShopCatService.get(id);
		wxShopCat.setDelFlag(1);
		wxShopCat.setUpAt(new Date());
		if(wxShopCatService.update(wxShopCat)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shop:wxShopCat:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				WxShopCatDO wxShopCat = wxShopCatService.get(string);
				wxShopCat.setDelFlag(1);
				wxShopCat.setUpAt(new Date());
				wxShopCatService.update(wxShopCat);
			}
		}
	
		return R.ok();
	}


	/**
	 * 获取类目并返回
	 */
	@PostMapping( "/getCatList")
	@ResponseBody
	@RequiresPermissions("shop:wxShopCat:wxShopCat")
	public R getSchoolList(@RequestParam Map<String, Object> params){
		JSONObject jsonObject = new JSONObject();
		JSONObject jsonObject2 = new JSONObject();
			/*//JSONObject jsonObject3 = new JSONObject();
			String schoolId = ShiroUtils.getUser().getSchoolId();
			if(StringUtils.isNoneBlank(schoolId)){
				params.put("schoolId", schoolId);
			}else{
				params.put("schoolId", schoolSchoolService.getPtSchool().getId());
			}*/
		if(params.containsKey("searchValue")&&params.get("searchValue")!=null){
			params.put("thirdCatId", params.get("searchValue").toString());
		}
		params.put("delFlag", "0");
		PageHelperUtils.page(params);
		List<WxShopCatDO> list = wxShopCatService.likeList(params);
		PageInfo<WxShopCatDO> pageInfo = new PageInfo<>(list);
		jsonObject.put("pageSize", pageInfo.getPageSize());
		jsonObject.put("pageNumber", pageInfo.getPageNum());
		jsonObject.put("totalRow", pageInfo.getTotal());//数据总数
		jsonObject.put("totalPage", pageInfo.getPages());//页码
		jsonObject.put("list",pageInfo.getList());//展示的数据
		jsonObject2.put("gridResult", jsonObject);//固定格式
		//jsonObject3.put("values", jsonObject2);//固定格式
		return R.ok().put("values", jsonObject2);
	}
	
}
